﻿#!/bin/sh
###############################################################################
##  Author    : yanjintao
##  Name      : edw_phoenix_executor.sh
##  Functions : Construct the interface of phoenix
##  Purpose   : Construct the interface of phoenix
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2017-07-23  yanjintao        1. CREATED THIS SHELL.
###############################################################################

. ${0%`basename ${0}`}edw_parm.sh
. ${0%`basename ${0}`}edw_func.sh

function USAGE(){
    echo -e "\nHow to use this shell script!"
    echo -e '\n${V_SHELL_HOME}/edw_phoenix_executor.sh -q Phoenix.DEL_DATA.sql'

}

#phoenix home
V_PHOENIX_HOME=/home/hadoop/dwhdp/core/phoenix/bin
#SQLFILE
V_SQLFILE_PATH="${V_SHELL_PROC}/phoenix_tmp"


#get param
V_FILE_NAME="$1"

#phoenix execute sql
function phoenix_exec_sql_file(){
	cd ${V_PHOENIX_HOME} && psql.py  "${V_SQLFILE_PATH}/${V_FILE_NAME}"
}

echo "${0}"

if [[ -f "${V_SHELL_PROC}/${V_FILE_NAME}" ]]; then
	#create the folder if not exist
	if [[ ! -d "${V_SQLFILE_PATH}" ]]; then
		#phoenix tmp dir
		mkdir -p "${V_SQLFILE_PATH}"
	fi

	#create a copy file 
	cp -f "${V_SHELL_PROC}/${V_FILE_NAME}"  "${V_SQLFILE_PATH}/${V_FILE_NAME}"

	#replace parm
	while read line ;do
       V_KEY=$(echo ${line} | awk -F'=' '{print $1}')
       V_VAL=$(echo ${line} | awk -F'=' '{print $2}')
       echo "${V_KEY}----${V_VAL}"
       sed -i "s/${V_KEY}/${V_VAL}/g" "${V_SQLFILE_PATH}/${V_FILE_NAME}"       
  done < "${V_SHELL_PARM}/ALL_ORG/edw_parm.txt"

    phoenix_exec_sql_file "${V_SQLFILE_PATH}/${V_FILE_NAME}"
fi

exit 0 

